package com.minminaya.source;

public class Algorithm_2 {

    /**
     * @param stringBuffer 当前字符
     */
    public static String replaceBlank(StringBuffer stringBuffer) {
        int oldLength = stringBuffer.length();
        for (int i = 0; i < oldLength; i++) {
            if (stringBuffer.charAt(i) == ' ') {
                //如果有一个空格，那么长度在stringBuffer后加入一空格长度占位
                stringBuffer.append(" ");
            }
        }

        //新老数组的指针
        int indexOfOld = oldLength - 1;
        int indexOfNew = stringBuffer.length() - 1;
        while (indexOfOld >= 0 && indexOfNew > indexOfOld) {
            char c = stringBuffer.charAt(indexOfOld--);
            if (c == ' ') {
                stringBuffer.setCharAt(indexOfNew--, '%');
                stringBuffer.setCharAt(indexOfNew--, '2');
                stringBuffer.setCharAt(indexOfNew--, '0');
            } else {
                stringBuffer.setCharAt(indexOfNew--, c);
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {

        System.out.println("Hello------");
        String string = "We are happy";
        StringBuffer stringBuffer = new StringBuffer(string);
        String temp = Algorithm_2.replaceBlank(stringBuffer);
        System.out.print("方案1当前字符为：" + temp);

    }
}
